package com.mdxx.xczd.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.TextView;
import com.mdxx.xczd.imp.TimeEnd;
import com.mdxx.xczd.utils.FormatUtil;
import com.orhanobut.logger.Logger;

public class TimerTextView extends TextView implements Runnable{

	private long mhour, mmin, msecond;
	private boolean run = false;
	private TimeEnd timeEnd;
	public TimerTextView(Context context) {
		this(context, null);
	}

	public TimerTextView(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public TimerTextView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	public void setTimes(long[] times) {
		mhour = times[0];
		mmin = times[1];
		msecond = times[2];
	}

	private void ComputeTime() {
	    msecond--;
	    if (msecond < 0) {
	        mmin--;
	        msecond = 59;
	        if (mmin < 0) {
	            mmin = 59;
	            mhour--;	            
	        }	
	    }	    
	}

    public boolean isRun() {
        return run;
    }

    public void beginRun() {
        this.run = true;
        run();
    }
    
    public void stopRun(){
    	this.run = false;
		timeEnd.timeEnd();
    }

	public void stop(){
		this.run = false;
	}

    @Override
    public void run() {
        if(run){
        	ComputeTime();
//            String strTime= mhour+":"+ mmin+":"+msecond;
        	 String strTime= FormatUtil.getformatStr(mhour, mmin, msecond);
            this.setText(strTime);
            if(mhour == 0 && msecond == 0 && mmin == 0){
    	    	msecond = 0;
    	    	mmin = 0;
    	    	mhour = 0;
				stopRun();
    	    }
            postDelayed(this, 1000);
        }else {
        	removeCallbacks(this);
        }
    }

	public void setTimeout(TimeEnd timeEnd){
		this.timeEnd = timeEnd;
	}

}
